﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 当前人物身上携带的技能
/// </summary>
public class SkillManager : MonoBehaviour
{
    /// <summary>
    /// 技能总列表
    /// </summary>
    public static List<Skill> skillList = new List<Skill>();
    /// <summary>
    /// 当前拥有的技能
    /// </summary>
    public static Dictionary<KeyCode, Skill> currentSkillDic = new Dictionary<KeyCode, Skill>();

    public GameObject slider;
    public GameObject sphereSkill;

    private bool effectOne = false;

    private static bool skillRelease = false;
    private static bool skillReady = true;

    private static Component obj;

    //如何判断技能的表现呢

    void Start()
    {
        obj = this.GetComponent<SkillManager>();
    }

    public static void AddCurrentSkill(int id)
    {
        Skill skill = SkillList.GetSkillById(id);
        //判断WER出是否有技能。
        if (!currentSkillDic.ContainsKey(KeyCode.W))
        {
            currentSkillDic.Add(KeyCode.W, skill);
        }
        else if (!currentSkillDic.ContainsKey(KeyCode.E))
        {
            currentSkillDic.Add(KeyCode.E, skill);
        }
        else if (!currentSkillDic.ContainsKey(KeyCode.R))
        {
            currentSkillDic.Add(KeyCode.R, skill);
        }
        else
        {
            //弹窗，技能已满。是否进行替换。

            //直接替换R技能栏
            currentSkillDic[KeyCode.R] = skill;
        }

        //发送UI事件，技能图标
        ObserverManager.ui_observer.Send(obj, UIEventType.AddSkill, currentSkillDic);
    }

    void Update()
    {
        //按下Q技能，先释放出技能条
        if (Input.GetKeyDown(KeyCode.Q))
        {
            slider.gameObject.SetActive(true);
            effectOne = true;
        }
        if (Input.GetKeyUp(KeyCode.Q))
        {
            effectOne = false;
            slider.gameObject.SetActive(false);
        }
        if (effectOne)
        {
            slider.transform.position = Input.mousePosition;
            if (Input.GetKeyDown(KeyCode.Mouse0))
            {
                Debug.Log("释放技能One");
                effectOne = false;
                slider.gameObject.SetActive(false);
            }
        }
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            if (effectOne)
            {
                effectOne = false;
                slider.gameObject.SetActive(false);
            }
        }

        

        //W技能释放
        if (Input.GetKey(KeyCode.W))
        {
            if (skillReady)
            {
                skillRelease = true;
                ReleaseSkills(KeyCode.W);
            }
        }
        if (Input.GetKeyUp(KeyCode.W))
        {
            skillRelease = false;
            skillReady = true;
        }

        //E技能释放
        if (Input.GetKey(KeyCode.E))
        {
            if (skillReady)
            {
                skillRelease = true;
                ReleaseSkills(KeyCode.E);
            }
        }
        if (Input.GetKeyUp(KeyCode.E))
        {
            skillRelease = false;
            skillReady = true;
        }

        //R技能释放
        if (Input.GetKey(KeyCode.R))
        {
            if (skillReady)
            {
                skillRelease = true;
                ReleaseSkills(KeyCode.R);
            }
        }
        if (Input.GetKeyUp(KeyCode.R))
        {
            skillRelease = false;
            skillReady = true;
        }
    }

    private void ReleaseSkills(KeyCode keyCode)
    {
        if (skillRelease)
        {
            if (currentSkillDic.ContainsKey(keyCode))
            {
                switch (currentSkillDic[keyCode].skillType)
                {
                    case SkillType.PointSkill:
                        PointSkill(keyCode);
                        break;
                    case SkillType.AreaSkill:
                        AreaSkill(keyCode);
                        break;
                    case SkillType.StraightLineSkill:
                        StrainghtLineSkill(keyCode);
                        break;
                    case SkillType.BuffSkill:
                        BuffSkill(keyCode);
                        break;
                    case SkillType.AdditionalSkill:
                        AdditionalSkill(keyCode);
                        break;
                }
            }
        }
    }

    #region 技能
    //技能释放，技能开始释放、技能结束释放。
    /// <summary>
    /// 鼠标左键点击。按下开始释放、抬起结束释放。CD期间无法释放。释放成功后技能消失
    /// </summary>
    private void PointSkill(KeyCode keyCode)
    {
        //更换鼠标样式
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;

            if (Physics.Raycast(ray, out hit))
            {
                if (hit.transform.tag.Equals("Player") && hit.transform.name != this.name)
                {
                    Debug.Log(hit.transform.name);
                    //可以释放技能
                    StartCoroutine(ReleasePointSkill(keyCode));
                }
            }
        }
        //
    }

    private IEnumerator ReleasePointSkill(KeyCode keyCode)
    {
        //释放动画
        //yield return new WaitForSeconds(2);
        //伤害计算

        //技能释放结束,更改鼠标样式。
        ReleaseSkillOver(keyCode);
        yield return 0;
    }

    /// <summary>
    /// 区域技能。 
    /// </summary>
    private void AreaSkill(KeyCode keyCode)
    {
        //
        sphereSkill.SetActive(true);
        sphereSkill.transform.position = Input.mousePosition;
        //更换鼠标样式
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            StartCoroutine(ReleaseAreaSkill(keyCode));
        }
    }
    /// <summary>
    /// 区域技能释放
    /// </summary>
    /// <returns></returns>
    private IEnumerator ReleaseAreaSkill(KeyCode keyCode)
    {
        //技能释放结束
        sphereSkill.SetActive(false);
        ReleaseSkillOver(keyCode);

        //释放技能特效
        GameObject windEffect = Instantiate(Resources.Load("Effects/CFX_Tornado") as GameObject);
        windEffect.transform.localRotation = Quaternion.Euler(-90, 0, 0);
        windEffect.transform.position = Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, 21f));
        windEffect.transform.position = new Vector3(windEffect.transform.position.x, 0, windEffect.transform.position.z);
        yield return new WaitForSeconds(1.5f);
        //Destroy(windEffect);
        //伤害判断
        yield return 0;
    }
    private void StrainghtLineSkill(KeyCode keyCode)
    {
        slider.gameObject.SetActive(true);
        //位置
        slider.transform.position = Input.mousePosition;
        //角度计算。
        Vector3 direction = Input.mousePosition - Camera.main.WorldToScreenPoint(GameObject.Find("Player").transform.position);
        float angle = Vector3.Angle(direction, new Vector3(0, -1, 0));

        slider.transform.localRotation = Quaternion.Euler(0, 0, -angle);
        if (Input.GetKeyDown(KeyCode.Mouse0))
        {
            StartCoroutine(ReleaseStrainghtLineSkill(keyCode));
        }
    }

    /// <summary>
    /// 直线技能释放
    /// </summary>
    /// <returns></returns>
    private IEnumerator ReleaseStrainghtLineSkill(KeyCode keyCode)
    {
        //释放技能特效

        //伤害计算
        //技能释放结束,更改鼠标样式。
        //销毁技能
        slider.gameObject.SetActive(false);
        ReleaseSkillOver(keyCode);

        yield return 0;
    }

    
    private void BuffSkill(KeyCode keyCode)
    {
        StartCoroutine(ReleaseBuffSkill(keyCode));
    }
    /// <summary>
    /// buff技能释放
    /// </summary>
    /// <param name="keyCode"></param>
    private IEnumerator ReleaseBuffSkill(KeyCode keyCode)
    {
        //技能特效释放。
        //buff加持属性。
        ReleaseSkillOver(keyCode);
        yield return 0;
    }
    private void AdditionalSkill(KeyCode keyCode)
    { 
    
    }
    /// <summary>
    /// 额外技能释放？
    /// </summary>
    /// <param name="keyCode"></param>
    /// <returns></returns>
    private IEnumerator ReleaseAdditionalSkill(KeyCode keyCode)
    {
        ReleaseSkillOver(keyCode);
        yield return 0;
    }

    private void ReleaseSkillOver(KeyCode keyCode)
    {
        skillRelease = false;
        skillReady = false;

        ObserverManager.ui_observer.Send(this, UIEventType.RemoveSkill, keyCode);
        currentSkillDic.Remove(keyCode);
    }
    #endregion
}

/// <summary>
/// 技能的种类
/// </summary>
public class Skill
{
    public int id { set; get; }
    public string name { set; get; }
    public SkillType skillType { set; get; }
    public string skillSprite { set; get; }
    public string des { set; get; }
    public Skill(int _id, string _name, SkillType _skillType, string _skillSprite, string _des)
    {
        this.id = _id;
        this.name = _name;
        this.des = _des;
        this.skillType = _skillType;
        this.skillSprite = _skillSprite;
    }
}

public enum SkillType
{ 
    PointSkill,  //指向技能

    AreaSkill,  //区域范围技能

    StraightLineSkill, //直线技能

    BuffSkill,  //buff技能

    AdditionalSkill,  //附加伤害技能？算在buff技能中？

}

